//
//  LZMyChangeNickNameView.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/10/28.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyChangeNickNameView.h"

#import "Masonry.h"
 

@interface LZMyChangeNickNameView() <UITextFieldDelegate>

@end

@implementation LZMyChangeNickNameView

- (instancetype)initWithIsPassword:(BOOL)isPassword{
    
    self = [super init];
    if (self) {
        
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        
        self.isPassword = isPassword;
        
        [self configView];
        
    }
    return self;
}

- (void)configView {
    
    WS(weakSelf);
    
    [self addSubview:self.textField];
    
    [self.textField makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.bottom.mas_equalTo(0);
        make.trailing.mas_equalTo(-5);
    }];
    
    if (self.isPassword) {
        [self.textField setSecureTextEntry:YES];
        [self addSubview:self.deleteButton];
        [self.deleteButton makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(weakSelf.centerY);
            make.trailing.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
    }
}

#pragma mark - Setter/Getter

- (UITextField *)textField {

    if (_textField == nil) {
        
        _textField = [[LZTextField alloc] init];
        _textField.placeholder      = @"Please enter a nickname";
        _textField.font             = [UIFont systemFontOfSize:14];
        _textField.delegate         = self;
        _textField.clearButtonMode  = UITextFieldViewModeWhileEditing;
        
        [_textField setValue:UIColorFromRGB(0xcccccc, 1) forKeyPath:@"_placeholderLabel.textColor"];
        [_textField setTintColor:UIColorFromRGB(0x33a928, 1)];
        [_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        
        UIButton * clearButton = [_textField valueForKey:@"_clearButton"];
        clearButton.backgroundColor = [UIColor clearColor];
        [clearButton setImage:[UIImage imageNamed:@"lz_mydeletepassword"] forState:UIControlStateNormal];
        [clearButton setImage:[UIImage imageNamed:@"lz_mydeletepassword"] forState:UIControlStateHighlighted];
    }
    
    return _textField;
}

- (UIButton *)deleteButton {

    if (_deleteButton == nil) {
        
        if (_deleteButton == nil) {
            _deleteButton = [[UIButton alloc] init];
            [_deleteButton setImage:[UIImage imageNamed:@"lz_myhidepassword"] forState:UIControlStateNormal];
            [_deleteButton setImage:[UIImage imageNamed:@"lz_myshowpassword"] forState:UIControlStateSelected];
            _deleteButton.hidden    = YES;
            _deleteButton.selected  = NO;
            [_deleteButton addTarget:self action:@selector(deleteButton:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    return _deleteButton;
}

- (void)deleteButton:(UIButton *)sender {

    if(sender.selected) {
        sender.selected = NO;
        [self.textField setSecureTextEntry:YES];
    }
    else {
        sender.selected = YES;
        [self.textField setSecureTextEntry:NO];
    }
}

- (void)textFieldDidChange:(UITextField *)sender {

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.deleteButton.hidden = YES;
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {

}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    self.deleteButton.hidden = NO;
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {

}

- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason {

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {

    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    return YES;
}


@end
